package com.nbicc.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nbicc.pojo.Record;
import com.nbicc.service.RecordService;
import com.nbicc.util.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName:
 * @Author: 仄言in
 * @Desctiption: (描述)
 * @Date: 2021/5/11 13:52
 * @Version 1.0 (版本)
 */
@RestController
@RequestMapping("/record")
public class RecordController {
    @Autowired
    private RecordService recordService;
    /**
     * 分页查询
     * @return
     */
    @GetMapping("/selectAll")
    public Map<Object,Object> selectAll(@RequestParam(value = "page",defaultValue = "1")Integer page,
                                        @RequestParam(value = "limit",defaultValue = "5")Integer pageSize){
        System.out.println("--selectAll--");
        HashMap<Object, Object> map = new HashMap<>();
        HashMap<Object, Object> map1 = new HashMap<>();
        PageHelper.startPage(page,pageSize);
        PageInfo<Record> pageInfo = recordService.selectAll();
        Double totalRowRecord = recordService.totalRowRecord();
        JsonData jsonData = new JsonData();
        map1.put("sum",totalRowRecord);
        map.put("code",0);
        map.put("msg","");
        map.put("count",(int)pageInfo.getTotal());
        map.put("data",pageInfo.getList());
        map.put("totalRow",map1);
        return map;
    }
    /**
     * 删除
     */
    @DeleteMapping("/deleteById/{id}")
    public JsonData deleteById(@PathVariable("id")Integer id){
        System.out.println("--deleteById--");
        JsonData jsonData = new JsonData();
        int i = recordService.deletById(id);
        if (i >0) {
            System.out.println("成功");
            return jsonData.buildSuccess();
        }else {
            System.out.println("失败");
            return jsonData.buildFailure();
        }
    }
    /**
     * 修改
     */
    @PutMapping("/updateById")
    public JsonData updateById(@RequestBody Record record){
        System.out.println("--updateById--");
        JsonData jsonData = new JsonData();
        int i = recordService.updateById(record);
        if (i >0) {
            System.out.println("成功");
            return jsonData.buildSuccess();
        }else {
            System.out.println("失败");
            return jsonData.buildFailure();
        }
    }
    /**
     * 添加
     */
    @PostMapping("/addRecord")
    public JsonData addFinan(@RequestBody Record record){
        System.out.println("--addRecord--");
        JsonData jsonData = new JsonData();
        int i = recordService.addFinan(record);
        if (i >0) {
            System.out.println("成功");
            return jsonData.buildSuccess();
        }else {
            System.out.println("失败");
            return jsonData.buildFailure();
        }
    }
    /**
     * 多条件查询
     */
    @GetMapping("/selectAllSB/{unitname}")
    public Map<Object,Object> selectAllSB(@RequestParam(value = "page",defaultValue = "1")Integer page,
                                          @RequestParam(value = "limit",defaultValue = "5")Integer pageSize,@PathVariable("unitname") String unitname){
        System.out.println("---selectAllSB---");
        HashMap<Object, Object> map = new HashMap<>();
        JsonData jsonData = new JsonData();
        PageHelper.startPage(page,pageSize);
        PageInfo<Record> pageInfo = recordService.selectAllSB(unitname);
        map.put("code",0);
        map.put("msg","");
        map.put("count",(int)pageInfo.getTotal());
        map.put("data",pageInfo.getList());
        return map;
    }
    /**
     * 单查询
     */
    @PostMapping("/selectById/{id}")
    public JsonData selectById(@PathVariable("id")Integer id){
        System.out.println("---selectById---");
        JsonData jsonData = new JsonData();
        Record record = recordService.selectById(id);
        if (record==null) {
            System.out.println("失败");
            return jsonData.buildFailure();
        }else {
            System.out.println("成功");
            return jsonData.buildSuccess(record);
        }
    }
}
